<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <RbtBread>
        <RbtBreadItem to="/">首页</RbtBreadItem>
        <Transition name="fade-right" mode="out-in">
          <RbtBreadItem :key="topCategory.id">{{
            topCategory.name
          }}</RbtBreadItem>
        </Transition>
      </RbtBread>
      <!-- 轮播图 -->
      <RbtCarousel :sliders="sliders" style="height: 500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in topCategory.children" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 -->
      <div v-for="item in subList" :key="item.id" class="ref-goods">
        <div class="head">
          <h3>-{{ item.name }}-</h3>
          <p class="tag">温暖柔软，品质之选</p>
          <RbtMore />
        </div>
        <div class="body">
          <GoodsItem v-for="g in item.goods" :key="g.id" :goods="g" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { findBanner } from '@/api/home'
import { findTopCategory } from '@/api/category'
import {
  BannerType,
  CateGoryList,
  CateGoryTypeChildren
} from '@/types/category'
import useStore from '@/store'
import { useRoute } from 'vue-router'

const route = useRoute()

// 轮播图
const sliders = ref<BannerType[]>([])
findBanner().then(res => {
  sliders.value = res.data.result
})

// 面包屑+轮播图
const { category } = useStore()
let topCategory = computed(() => {
  let cate: CateGoryList = {}
  const item = category.$state.list.find(item => {
    return item.id === route.params.id
  })
  if (item) cate = item
  return cate
})

// 推荐商品
const subList = ref<CateGoryTypeChildren[]>([])
const getSubList = () => {
  findTopCategory(route.params.id as string).then(res => {
    subList.value = res.data.result.children
  })
}

watch(
  () => route.params.id,
  newVal => {
    if (newVal && `/category/${newVal}` === route.path) getSubList()
  },
  { immediate: true }
)
</script>

<style scoped lang="scss">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: $rbtColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .rbt-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
}
</style>
